/*
 * VistaHorario.java
 *
 * Created on 16/02/2011, 07:48:08 PM
 * Pedro A. Anaya Avila 
 */
package Matricula_Nota;

import java.util.ArrayList;
import javax.swing.DefaultListSelectionModel;
import javax.swing.JOptionPane;
import javax.swing.table.DefaultTableModel;

/**
 * @author Pedro Anaya
 */
public class VistaHorario extends javax.swing.JDialog {

    private Coordinador coordinador;
    private Seccion seccion;
    private DefaultTableModel mdlHorario;
    private ArrayList<String> dniProfesores;
    private int estado = 0;

    /** Creates new form VistaHorario */
    public VistaHorario(javax.swing.JDialog parent, boolean modal, Coordinador coordinador, Seccion seccion) {
        super(parent, modal);
        initComponents();
        this.coordinador = coordinador;
        this.seccion = seccion;
        dniProfesores = new ArrayList<String>();


        mdlHorario = (DefaultTableModel) tblHorario.getModel();
        tblHorario.setSelectionMode(DefaultListSelectionModel.SINGLE_SELECTION);
        try {
            mostrarAreas();
        } catch (RuntimeException ex) {
            JOptionPane.showMessageDialog(rootPane, ex.getMessage());
                        this.dispose();
            estado = 1;
        }
        mostrarHorarios();
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jLabel2 = new javax.swing.JLabel();
        jPanel1 = new javax.swing.JPanel();
        jLabel4 = new javax.swing.JLabel();
        cboCurso = new javax.swing.JComboBox();
        jLabel5 = new javax.swing.JLabel();
        cboProfesor = new javax.swing.JComboBox();
        jLabel6 = new javax.swing.JLabel();
        cboDias = new javax.swing.JComboBox();
        jLabel7 = new javax.swing.JLabel();
        cboHora = new javax.swing.JComboBox();
        btnRegistrar = new javax.swing.JButton();
        jLabel3 = new javax.swing.JLabel();
        cboArea = new javax.swing.JComboBox();
        jScrollPane2 = new javax.swing.JScrollPane();
        tblHorario = new javax.swing.JTable();

        jLabel2.setText("jLabel2");

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
        setTitle("Registrar Horario");
        addWindowListener(new java.awt.event.WindowAdapter() {
            public void windowOpened(java.awt.event.WindowEvent evt) {
                formWindowOpened(evt);
            }
        });

        jPanel1.setBorder(javax.swing.BorderFactory.createTitledBorder("Horario"));

        jLabel4.setText("Curso");

        cboCurso.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                cboCursoActionPerformed(evt);
            }
        });

        jLabel5.setText("Profesor");

        jLabel6.setText("Día");

        cboDias.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "Lunes", "Martes", "Miércoles", "Jueves", "Viernes" }));

        jLabel7.setText("Hora");

        cboHora.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "1", "2", "3", "4", "5", "6", "7" }));

        btnRegistrar.setIcon(new javax.swing.ImageIcon(getClass().getResource("/Matricula_Nota/Recursos/Save.png"))); // NOI18N
        btnRegistrar.setToolTipText("Registrar Horario");
        btnRegistrar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnRegistrarActionPerformed(evt);
            }
        });

        jLabel3.setText("Área");

        cboArea.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                cboAreaActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jLabel4)
                            .addComponent(jLabel3))
                        .addGap(18, 18, 18)
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(cboCurso, javax.swing.GroupLayout.PREFERRED_SIZE, 100, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(cboArea, javax.swing.GroupLayout.PREFERRED_SIZE, 157, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addGap(41, 41, 41)
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jLabel6)
                            .addComponent(jLabel7))
                        .addGap(36, 36, 36)
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(cboHora, javax.swing.GroupLayout.PREFERRED_SIZE, 51, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(cboDias, javax.swing.GroupLayout.PREFERRED_SIZE, 70, javax.swing.GroupLayout.PREFERRED_SIZE)))
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addComponent(jLabel5)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(cboProfesor, javax.swing.GroupLayout.PREFERRED_SIZE, 174, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(btnRegistrar, javax.swing.GroupLayout.PREFERRED_SIZE, 25, javax.swing.GroupLayout.PREFERRED_SIZE)))
                .addContainerGap(96, Short.MAX_VALUE))
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel3)
                    .addComponent(cboArea, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel6)
                    .addComponent(cboDias, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel4)
                    .addComponent(cboCurso, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel7)
                    .addComponent(cboHora, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(18, 18, 18)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                        .addComponent(jLabel5)
                        .addComponent(cboProfesor, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addComponent(btnRegistrar))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        tblHorario.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {
                {"1", null, null, null, null, null},
                {"2", null, null, null, null, null},
                {"3", null, null, null, null, null},
                {"4", null, null, null, null, null},
                {"5", null, null, null, null, null},
                {"6", null, null, null, null, null},
                {"7", null, null, null, null, null}
            },
            new String [] {
                "Hora", "Lunes", "Martes", "Miércoles", "Jueves", "Viernes"
            }
        ) {
            Class[] types = new Class [] {
                java.lang.String.class, java.lang.String.class, java.lang.String.class, java.lang.String.class, java.lang.String.class, java.lang.String.class
            };
            boolean[] canEdit = new boolean [] {
                true, false, false, false, false, false
            };

            public Class getColumnClass(int columnIndex) {
                return types [columnIndex];
            }

            public boolean isCellEditable(int rowIndex, int columnIndex) {
                return canEdit [columnIndex];
            }
        });
        jScrollPane2.setViewportView(tblHorario);

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jScrollPane2, javax.swing.GroupLayout.DEFAULT_SIZE, 491, Short.MAX_VALUE)
                    .addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(11, 11, 11)
                .addComponent(jScrollPane2, javax.swing.GroupLayout.PREFERRED_SIZE, 188, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void btnRegistrarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnRegistrarActionPerformed
        try {
            Area area = coordinador.buscarNombreArea(cboArea.getSelectedItem().toString());
            Curso curso = coordinador.buscarNombreCurso(cboCurso.getSelectedItem().toString(), area);
            Profesor profesor = coordinador.buscarProfesor(dniProfesores.get(cboProfesor.getSelectedIndex()));
            coordinador.registrarHorario(cboDias.getSelectedIndex() + 1, cboHora.getSelectedIndex() + 1, seccion, curso, profesor);
            mostrarHorarios();
        } catch (Exception Ex) {
            JOptionPane.showMessageDialog(rootPane, Ex.getMessage());
        }
}//GEN-LAST:event_btnRegistrarActionPerformed

    private void cboAreaActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_cboAreaActionPerformed
        // TODO add your handling code here:
        try {
            cboCurso.removeAllItems();
            mostrarCursos(coordinador.buscarNombreArea(cboArea.getSelectedItem().toString()));
            System.out.println("Se llamo al metodo cboArea");
        } catch (NullPointerException e) {
            System.out.println("Nulos");
        } catch (RuntimeException ex) {
            JOptionPane.showMessageDialog(rootPane, ex.getMessage());
            this.dispose();
        }

    }//GEN-LAST:event_cboAreaActionPerformed

    private void cboCursoActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_cboCursoActionPerformed
        // TODO add your handling code here:
        cboProfesor.removeAllItems();
        try {
            Area area = coordinador.buscarNombreArea(cboArea.getSelectedItem().toString());
            Curso curso = coordinador.buscarNombreCurso(cboCurso.getSelectedItem().toString(), area);
            mostrarProfesores(curso);
            System.out.println("Se llamo al metodo cboCurso");
        } catch (NullPointerException e) {
            System.out.println("Nulos");
        } catch (RuntimeException ex) {
            JOptionPane.showMessageDialog(rootPane, ex.getMessage());
            this.dispose();
        }
    }//GEN-LAST:event_cboCursoActionPerformed

    private void formWindowOpened(java.awt.event.WindowEvent evt) {//GEN-FIRST:event_formWindowOpened
        // TODO add your handling code here:
        if (estado == 1) {
            this.dispose();
        }
    }//GEN-LAST:event_formWindowOpened
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton btnRegistrar;
    private javax.swing.JComboBox cboArea;
    private javax.swing.JComboBox cboCurso;
    private javax.swing.JComboBox cboDias;
    private javax.swing.JComboBox cboHora;
    private javax.swing.JComboBox cboProfesor;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JLabel jLabel5;
    private javax.swing.JLabel jLabel6;
    private javax.swing.JLabel jLabel7;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JScrollPane jScrollPane2;
    private javax.swing.JTable tblHorario;
    // End of variables declaration//GEN-END:variables

    private void mostrarAreas() {
        ArrayList<Area> areas = coordinador.getAreas();
        for (Area area : areas) {
            cboArea.addItem(area.getNombre());
        }
        if (cboArea.getItemCount() <= 0) {
            throw new RuntimeException("NO HAY ÁREAS DISPONIBLES.");
        }
    }

    private void mostrarCursos(Area area) {
        ArrayList<Curso> cursos = area.getCursos();
        for (Curso curso : cursos) {
            cboCurso.addItem(curso.getNombre());
        }
        if (cboCurso.getItemCount() <= 0) {
            throw new RuntimeException("NO HAY CURSOS DISPONIBLES.");
        }
    }

    private void mostrarProfesores(Curso curso) {
        dniProfesores.clear();
        ArrayList<Profesor> profesores = curso.getProfesores();
        for (Profesor profesor : profesores) {
            cboProfesor.addItem(profesor.getNombre());
            dniProfesores.add(profesor.getDni());
        }
        if (cboProfesor.getItemCount() <= 0) {
            throw new RuntimeException("NO HAY PROFESORES");
        }
    }

    private void mostrarHorarios() {
        mdlHorario.setRowCount(7);
        for (int i = 0; i < seccion.getHorarios().size(); i++) {
            Horario horario = seccion.getHorarios().get(i);
            mdlHorario.setValueAt(horario.getCurso().getNombre(), horario.getHora() - 1, horario.getDia());
        }
    }
}
